//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { Toast } from "vant";
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
  return {
    query:{
      id:'',
      detail:'',
      image_list:[]
    },
    pluginId:'',
    goodsId:'',
    orderId:'',
    actId:'',
    reissueId:'',
    imgs:[],
    fileList3:[],
    uploadUrl:'',
    content:'',
    upNumMaxOther:0,
    coThemeColor:'',
    iPnoneBottomBol:false,
    taskShow:false,
    imgPath:'',
    
  };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    // 判断多图上传，循环onRead_1方法
    multipleMethod_1(e, flag) {
      if (e && e.length) {
        for (let i = 0; i < e.length; i++) {
          this.onRead_2(e[i], flag);
        }
      } else {
        this.onRead_2(e, flag);
      }
    },
    removeImg_1(delIndex) {
      this.fileList3.splice(delIndex, 1);
      this.query.image_list.splice(delIndex, 1);
      this.upNumMaxOther = this.fileList3.length;
      // this.$emit('update:imgs', this.imgs)
    },
    //跳转售后
    refundOn() {
      this.$router.push(
        this.fun.getUrl(
          "refund",
          {
            order_id: this.orderId,
            order_type: this.pluginId,
          },
          {orderType:'rebate',partId:this.query.id,goodsId:this.goodsId}
        )
      );
    },
    // vant 多其他图片上传
    onRead_2(e, flag) {
        var That = this;
        let fd = new FormData();
        if (this.upNumMaxOther == 10 || this.upNumMaxOther >= 10) {
          this.$toast('图片数量已达到上限')
          return false;
        }
        if (flag === true) {
          fd.append("file", e);
        } else {
          fd.append("file", e.file); // 第一个参数字符串可以填任意命名，第二个根据对象属性来找到file
        }
        fd.append("attach", "upload");

        // 添加加载提示
        Toast.loading({
          message: " ",
          forbidClick: true
        });
        axios
          .post(this.uploadUrl, fd, {
            headers: { "Content-Type": "multipart/form-data" }
          })
          .then(response => {
            var responseData = response.data;
            if (responseData.result == 1) {
              That.upNumMaxOther++;
              That.imgs.push(responseData.data.img_url);
              if (flag === true) {
                That.fileList3.push(e)
              } else {
                That.fileList3.push(e.file)
              }
              That.query.image_list = That.imgs;
              Toast.clear();
            } else {
              Toast(responseData.msg);
              Toast.clear();
            }
          })
          .catch(error => {
            console.log(error);
            Toast.clear();
          });
    },
    clockForm() {
      if (this.fun.isTextEmpty(this.query.detail)) {
        Toast("请填写内容");
        return;
      }
      if (this.fun.isTextEmpty(this.query.image_list)) {
        Toast("请上传图片");
        return;
      }
      //补卡发布打卡的逻辑
      if(this.actId) {
        let params = {
          check_in_id:this.actId,
          detail:this.query.detail,
          image_list:this.query.image_list
        }
        $http.post("plugin.check-in-rebate.frontend.participate-in.supplement", params, "正在获取").then(
          response => {
            let res = response.data;
            if (response.result == 1) {
              this.$toast(response.msg);
              if(res.is_give) {
                this.taskShow = true
              } else {
                this.goBack();
              }
            } else {
              if(res.type == 'no_supplement') {
                this.$dialog.confirm({
                  message: '可购买指定商品卡',
                })
                .then(() => {
                  // on confirm
                  // this.verification = true;
                  this.$router.push(this.fun.getUrl("goods", { id: this.reissueId }));
                })
                .catch(() => {
                  // on cancel
                });
              } else {
                this.$toast(response.msg);
              }
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      } else {
        //正常打卡
        $http.post("plugin.check-in-rebate.frontend.participate-in.check-in",this.query, "正在获取").then(
          response => {
            if (response.result == 1) {
              let res = response.data;
              console.log(res);
              if(res.is_give) {
                this.taskShow = true
              } else {
                this.goBack();
              }
            } else {
              
              this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      }
      
    },
    
  goBack() {
    if (window.history.length <= 1) {
      this.$router.push(this.fun.getUrl("home", {}));
    } else {
      this.$router.go(-1);
    }
  },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    //正常打卡
    if(this.$route.query.id) {
      this.query.id = this.$route.query.id;
      this.pluginId = this.$route.query.pluginId;
      this.goodsId= this.$route.query.goodsId;
      this.orderId=  this.$route.query.orderId;
    }
    //补卡的ID
    if(this.$route.query.actId) {
      this.actId = this.$route.query.actId;
      this.reissueId = this.$route.query.reissueId;
    }
    this.taskShow = false;
    this.query.detail = '';
    this.query.image_list = [];
    let yz_basic_info = JSON.parse(localStorage.getItem("yz_basic_info"));
    this.imgPath = yz_basic_info.host;
    this.coThemeColor = yz_basic_info.theme_color || "";
    this.uploadUrl = this.fun.getRealUrl("upload.uploadPic", {});
  }, //如果页面有keep-alive缓存功能，这个函数会触发
}